<?php
 mb_internal_encoding("utf-8");
//die();
/*
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";
*/
// require "../string/class_suffixarray.php"
 require "../string/class_suffixarray_new.php";
 require "../string/suffix_array.php";
//$corpus=Tool_corpus::get_corpus_regexp("../xml/europa_complete_repair/","fr");
//$corpus=Tool_corpus::get_corpus_regexp("../xml/test/","fr");
//print_r($corpus);
//$array_doc=$corpus->__get_array_doc();

$start=microtime(true);
$suffix_array=new suffixarray();
//$str1="mississippi";
//$str2="mississippi";
$str1="toto";
//$str2="tu";
$suffix_array->__add_str($str1);
//$suffix_array->__add_str($str2);
 $array_res=$suffix_array->__init();
 $end=microtime(true);
 $time=$end-$start;
// echo "final time : ".$time."\n";
//die();
// $array_str=$suffix_array->__get_array_str();   
 foreach($array_res as $c=>$rstr){
   $str=$suffix_array->__get_suffix($rstr[1][0]);
   $global_str=$suffix_array->__get_str($str[2]);
   $graphie=mb_substr($global_str,$str[0],$rstr[0]);
   echo $graphie."\n";
   foreach($rstr[1] as $pos){
     $su=$suffix_array->__get_suffix($pos);
     echo "    $su[0] chaine $su[2]\n";
   }
 }

 die();
 $res=array();
 foreach($array_res as $str=>$array_seg){$res[$str]=count($array_seg);}
 arsort($res);

 foreach($res as $str=>$count){echo "$count [$str]\n";}
die();


/*
$str1="rtt";
$str2="rttot";

$seg1=new segment($str1,0,strlen($str1));
$seg2=new segment($str2,0,strlen($str2));

echo $seg1;
echo "\n";
echo $seg2;
echo "\n";
echo Tool_segment::get_same_suffix($seg1,$seg2);

echo $seg2->__get_reverse_tostring();
die();
*/
//$all=Tool_dir::dir_getAll("./corpusd/c1/");

 $corpus=new corpus();
 $corpus->__init("./corpus/c1/");

//foreach($corpus->__get_array_doc() as $doc){
// $content=$doc->__get_container();
 $array_doc=$corpus->__get_array_doc();
//foreach($array_doc as $id=>$doc){
//  $suffix_array->__add_str($doc->__get_container());
//}
//die();
// $suffix_array->__add_str($array_doc[0]->__get_container());
// $suffix_array->__add_str($array_doc[0]->__get_container());
// $suffix_array->__add_str($array_doc[0]->__get_container());
//}

 $str1=Tool_files::file_load("./corpus/c1/07.c");
 $suffix_array->__add_str($str1);
// $suffix_array->__add_str($str1);
// $str1="abc";
// $suffix_array->__add_str($str1);
// $suffix_array->__add_str($str1);
// $suffix_array->__add_str($str1);
// $str2=Tool_files::file_load("./07.c");


// $str1="aza";
// $str2="azb";
 //$str1="ababa";
 //$str2="ababa";
//$str1=mb_ereg_replace(" ","s",$str1);
//$str1=mb_ereg_replace("\t","T",$str1);
//$str1=mb_ereg_replace("\n","N",$str1);

// $suffix_array->__add_str($str2);

 $start=microtime(true);
 $array_res=$suffix_array->__init();
 $end=microtime(true);
 $time=$end-$start;
// echo "time : $time \n\n";

 $array_str=$suffix_array->__get_array_str();   

// $str1="testest";
// print_r($suffix_array);
// die();
$res=array();
foreach($array_res as $str=>$array_seg){
//if((count($array_seg)%2)!=0){
// echo "************\n";
 //foreach($array_seg as $seg) echo "  ".$seg->__tostring_verbose()."\n";
  $res[$str]=count($array_seg);
// }
//}
}
//echo $segment->__tostring_verbose();

arsort($res);

foreach($res as $str=>$count){
  echo "$count\n[$str]\n";
}

die();

 foreach($array_res as $rstr=>$res_id_str){
    echo "================\n";
    echo $rstr;
    echo "\n";
    foreach($res_id_str as $id_str => $res_id_occ){
     //echo $id_str;
      foreach($res_id_occ as $segment){
       echo "\n";
      }
    }
    //print_r($res);
 }



// print_r($suffix_array);
// die();
